<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/25
 * Time: 11:26
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Repair extends Base
{
    /**
     * 报事报修类型首页
     * @return mixed|\think\response\Json
     */
    public function typeIndex(){
        if(request()->isAjax ()){
            $rt_name = trim(input('rt_name'), ' ');
            $where['rt_status'] = ['IN', '1,2'];
            if(isset($rt_name) && $rt_name != ''){
                $where['rt_name'] = ['like', '%' . $rt_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('repair_type')->where($where)->count();// 获取总条数
            $lists = Db::name('repair_type')
                ->where($where)
                ->field('rt_id, rt_name, rt_status, rt_time')
                ->page($Nowpage,$limits)
                ->order('rt_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('repair/typeindex');
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            $rt_name = trim(input('rt_name'), ' ');
            $insert = [
                'rt_name' => $rt_name,
                'rt_time' => date('Y-m-d H:i:s'),
                'rt_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_repair_type')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('repair/typeadd');
    }
    
    /**
     * 编辑分类
     * @return mixed|\think\response\Json
     */
    public function typeEdit(){
        $rt_id = input('rt_id');
        if(Request::instance()->isPost()){
            $name = trim(input('rt_name'), ' ');
            $update = [
                'rt_name' => $name,
                'rt_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_repair_type')->where('rt_id', $rt_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $rt_name = Db::table('up_repair_type')->where('rt_id', $rt_id)->value('rt_name');
        $this->assign([
            'rt_id' => $rt_id,
            'rt_name' => $rt_name
        ]);
        return $this->fetch('repair/typeedit');
    }
    
    /**
     * 禁用
     * @return \think\response\Json
     */
    public function typeDown(){
        $rt_id = input('id');
        $res = Db::table('up_repair_type')->where('rt_id', $rt_id)->setField('rt_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function typeOn(){
        $rt_id = input('id');
        $res = Db::table('up_repair_type')->where('rt_id', $rt_id)->setField('rt_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function typeDel(){
        $rt_id = input('id');
        $res = Db::table('up_repair_type')->where('rt_id', $rt_id)->setField('rt_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上报记录列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $repair_username = trim(input('repair_username'), ' '); // 上报用户名
            $repair_done_username = trim(input('repair_done_username'), ' '); // 接受管理员用户名
            $repair_done_phone = trim(input('repair_done_phone'), ' '); // 接受管理员联系方式
            $repair_type = input('repair_type'); // 事件类型
            $repair_plot = input('repair_plot');
            $where = [];
            
            if(isset($repair_username) && $repair_username != ''){
                $where['uuu1.user_username'] = ['like', '%' . $repair_username . '%'];
            }
            if(isset($repair_done_username) && $repair_done_username != ''){
                $where['uuu2.user_username'] = ['like', '%' . $repair_done_username . '%'];
            }
            if(isset($repair_done_phone) && $repair_done_phone != ''){
                $where['uuu2.user_phone'] = ['like', '%' . $repair_done_phone . '%'];
            }
            if(isset($repair_type) && $repair_type != ''){
                $where['repair_type_id'] = $repair_type;
            }
            if(isset($repair_plot) && $repair_plot != ''){
                $where['repair_plot_id'] = $repair_plot;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('repair')
                ->join('up_u_user uuu1', 'uuu1.user_id = repair_user_id', 'left')
                ->join('up_u_user uuu2', 'uuu2.user_id = repair_done_user_id', 'left')
                ->join('up_xq', 'xq_id = repair_plot_id', 'left')
                ->join('up_repair_type', 'rt_id = repair_type_id', 'left')
                ->join('up_repair_comment', 'rc_repair_id = repair_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('repair')
                ->join('up_u_user uuu1', 'uuu1.user_id = repair_user_id', 'left')
                ->join('up_u_user uuu2', 'uuu2.user_id = repair_done_user_id', 'left')
                ->join('up_xq', 'xq_id = repair_plot_id', 'left')
                ->join('up_repair_type', 'rt_id = repair_type_id', 'left')
                ->join('up_repair_comment', 'rc_repair_id = repair_id', 'left')
                ->where($where)
                ->field('repair_id, uuu1.user_username, xq_name, repair_phone, rt_name, repair_addr, 
                repair_explain, repair_status, repair_time, uuu2.user_username done_username, 
                uuu2.user_phone done_user_phone, rc_content, rc_type')
                ->page($Nowpage,$limits)
                ->order('repair_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $plot = Db::table('up_xq')->field('xq_id, xq_name')->select();
        $type = Db::table('up_repair_type')->field('rt_id, rt_name')->where('rt_status', 1)->select();
        $this->assign([
            'plot' => $plot,
            'repair_type' => $type
        ]);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function repairDetail(){
        $repair_id = input('repair_id');
        $detail = Db::table('up_repair')
            ->join('up_u_user uuu1', 'uuu1.user_id = repair_user_id', 'left')
            ->join('up_u_user uuu2', 'uuu2.user_id = repair_done_user_id', 'left')
            ->join('up_xq', 'xq_id = repair_plot_id', 'left')
            ->join('up_repair_type', 'rt_id = repair_type_id', 'left')
            ->join('up_repair_comment', 'rc_repair_id = repair_id', 'left')
            ->field('repair_id, uuu1.user_username, xq_name, uuu1.user_phone, repair_phone, rt_name, 
            repair_addr, repair_explain, repair_status, repair_time, uuu2.user_username done_username,
            uuu2.user_phone done_user_phone, rc_content, rc_type, repair_img, repair_do_time, repair_over_time')
            ->where('repair_id', $repair_id)
            ->find();
        $img = trim($detail['repair_img'], ',');
        $detail['repair_img'] = explode(',', $img);
        return $this->fetch('repair/repairdetail', ['repair' => $detail]);
    }
    
    /**
     * 评论列表
     * @return mixed|\think\response\Json
     */
    public function repairComment(){
        if(request()->isAjax ()){
            $rc_type = input('rc_type');
            $where = [];
            
            if(isset($rc_type) && $rc_type != ''){
                $where['rc_type'] = $rc_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('repair_comment')
                ->join('up_u_user', 'user_id = rc_user_id', 'left')
                ->join('up_repair', 'repair_id = rc_repair_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('repair_comment')
                ->join('up_u_user', 'user_id = rc_user_id', 'left')
                ->join('up_repair', 'repair_id = rc_repair_id', 'left')
                ->where($where)
                ->field('repair_explain, rc_type, user_username, rc_content, rc_time')
                ->page($Nowpage,$limits)
                ->order('rc_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        
        return $this->fetch('repair/repaircomment');
    }
}